<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/images/image.png" />
            <div class="title">在线判题系统</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div v-if="store.state.user.loginUser.id">
        <a-dropdown :trigger="['click']">
          <div class="avatar-dropdown">
            <a-avatar
              :image-url="
                store.state.user.loginUser.userAvatar ??
                require('@/assets/logo.png')
              "
            />
            <span
              class="user-name"
              style="width: 100px; text-align: center; display: inline-block"
            >
              {{ store.state.user.loginUser.userName ?? "无名" }}
            </span>
          </div>
          <icon-down />

          <template #content>
            <a-doption @click="doMenuClick('/about')">个人中心</a-doption>
            <a-doption @click="doMenuClick('/my/complete')">我的比赛</a-doption>
            <a-doption @click="doMenuClick('/my/date')">刷题日历</a-doption>
            <a-doption @click="handleLogOutClick()">退出登录</a-doption>
          </template>
        </a-dropdown>
      </div>
      <div v-else>
        <a-button type="primary" @click="handleLoginClick()">登录</a-button>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService, UserLoginRequest } from "../../generated";
import { Message } from "@arco-design/web-vue";
const router = useRouter();
const store = useStore();
const handleLoginClick = () => {
  // 处理退出登录逻辑
  // console.log("退出登录");
  router.push({
    path: "/user/login",
  });
};

const handleLogOutClick = async () => {
  // 处理退出登录逻辑
  const userLogOutResponse = await UserControllerService.userLogoutUsingPost();
  console.log(userLogOutResponse);
  if (userLogOutResponse.code === 0) {
    router.push({
      path: "/user/login",
    });
  } else {
    Message.error("网络异常");
  }
};
// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

// 默认主页
const selectedKeys = ref(["/"]);

// 路由跳转后，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

console.log();

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.avatar-dropdown {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-right: 120px;
}
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
